import { BaseMgr } from "../base/BaseMgr";
import BaseData from "../mvc/BaseData";

export class DataMgr extends BaseMgr {
    public static get inst() {
        return this.getInstance();
    }

    datas: { [module: number|string]: BaseData } = {};

    get<T extends BaseData>(module: number|string) {
        const data = this.datas[module];
        if (!data) {
            console.warn("[DataMgr] data模块未注册", module);
        }
        return data as T;
    }

    registerData<T extends BaseData>(module: number|string, dataClass: new () => T) {
        const data = new dataClass();
        this.datas[module] = data;
        return data;
    }

    update(dt: number) {
        for (let i in this.datas) {
            const data = this.datas[i];
            data.update(dt);
        }
    }

    destroy(): void {
        for (let i in this.datas) {
            const ctrl = this.datas[i];
            ctrl.onDestroy();
        }
        this.datas = {};
    }
}